<?php

namespace app\api\service\intelligentlive;

use app\api\model\intelligentlive\Account;
use app\lib\exception\intelligentlive\IntelligentliveException;
use think\Db;
use think\Exception;

class AccountService
{
    private static $instance;

    public static function getInstance(): AccountService
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * @param('buyin_id','百应id','require')
     */
    public function addAccount($params)
    {
        $info = [
            "account"=>$params['account']??"",
            "password"=>$params['password']??"",
            "name"=>$params['nickname'],
            "app_id"=>$params['buyin_id'],
            "platform_id"=> 7,
            "remark"=>$params['remark']??null,
            "class"=>$params['class']??"",
            "director"=>$params['director']??"",
            "accountname"=>$params['accountname']??"",
            "douyin_id"=>$params['douyin_id']??"",
            "position"=>$params['position']??"",
            "mobile"=>$params['mobile']??"",
            "realname"=>$params['realname']??"",
            "matrix"=>$params['matrix']??"",
            "credit_score"=>$params['credit_score']??"",
            "violation_list"=>$params['violation_list']??"",
            "belongto"=>$params['belongto']??"",
            "security_deposit"=>$params['security_deposit']??"",
            "seller_rating"=>$params['seller_rating']??"",
            "total_fans"=>$params['total_fans']??"",
            "withdraw_balance"=>$params['withdraw_balance']??"",
        ];

        $account = Account::where("app_id",$params['buyin_id'])->find();
        if ($account){
            throw new IntelligentliveException([
                'msg' => "渠道账号已存在，请勿重复提交",
                'error_code' => 20001
            ]);
        }
        DB::startTrans();
        try {
            Account::create($info);
            // 提交事务
            Db::commit();
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            throw new IntelligentliveException([
                'msg' => "添加失败",
                'error_code' => 20002
            ]);
        }
    }

    /**
     * @param('buyin_id','百应id','require')
    */
    public function upAccount($params)
    {
//        $info = [
//            "account"=>$params['account']??"",
//            "password"=>$params['password']??"",
//            "name"=>$params['nickname'],
////            "app_id"=>$params['buyin_id'],
////            "platform_id"=>$params['platform_id'],
//            "remark"=>$params['remark']??null,
//            "in_use"=>$params['in_use']??0,
//            "class"=>$params['class']??"",
//            "director"=>$params['director']??"",
//            "accountname"=>$params['accountname']??"",
//            "douyin_id"=>$params['douyin_id']??"",
//            "position"=>$params['position']??"",
//            "mobile"=>$params['mobile']??"",
//            "realname"=>$params['realname']??"",
//            "matrix"=>$params['matrix']??"",
//            "credit_score"=>$params['credit_score']??"",
//            "violation_list"=>$params['violation_list']??"",
//            "belongto"=>$params['belongto']??"",
//            "security_deposit"=>$params['security_deposit']??"",
//            "seller_rating"=>$params['seller_rating']??"",
//            "total_fans"=>$params['total_fans']??"",
//            "withdraw_balance"=>$params['withdraw_balance']??"",
//        ];
        //兼容前端
        if (!empty($params["nickname"])) {
            $params["name"] = $params["nickname"];
            unset($params["nickname"]);
        }
        $account = Account::where("platform_id",7)->where("app_id",$params['buyin_id'])->findOrEmpty();
        if ($account->isEmpty()){
            throw new IntelligentliveException([
                'msg' => "账号不存在",
                'error_code' => 20001
            ]);
        } else {
            DB::startTrans();
            try {
                unset($params["buyin_id"]);
                Account::where("platform_id",7)->where("id",$account['id'])->update($params);
                // 提交事务
                Db::commit();
            }catch (Exception $e){
                // 回滚事务
                Db::rollback();
                throw new IntelligentliveException([
                    'msg' => "更新失败",
                    'error_code' => 20003
                ]);
            }
        }

    }

    public function getAccount($params)
    {
//        $params['name'] = "妙姐的日常";
        $query = Account::field("*")
            ->with("liveData")
            ->where("platform_id",7)
            ->where("cookie",'not null');

        if (!empty($params["name"])) {
            $query->where('name',$params['name']);
        } else {
            $query->where("name","NOT IN", ['诗情画意', '一诗二画机构']);
        }

        $account = $query->order("id", "desc")->select()->toArray();
        return $account;
    }
}